<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['title', validatorRules.title]" placeholder="请输入标题"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="展示图" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['plan', validatorRules.plan]"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="方案列表" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['programlist', validatorRules.programlist]" placeholder="请输入方案列表"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="banner图" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['banner', validatorRules.banner]"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="分析描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['analysisdepict', validatorRules.analysisdepict]" rows="4"
                          placeholder="请输入分析描述"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="方案描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea v-decorator="['programdepict', validatorRules.programdepict]" rows="4" placeholder="请输入方案描述"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="分析列表" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['analysislist', validatorRules.analysislist]" placeholder="请输入分析列表"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { getAction, httpAction } from '@/api/manage'
  import pick from 'lodash.pick'

  export default {
    name: 'MgmSolutionForm',
    components: {},
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: () => {
        },
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          createtime: {
            rules: [
              { required: true, message: '请输入创建时间!' },
            ]
          },
          updatetime: {
            rules: [
              { required: true, message: '请输入更新时间!' },
            ]
          },
          title: {
            rules: [
              { required: true, message: '请输入标题!' },
            ]
          },
          plan: {
            rules: [
              { required: true, message: '请输入展示图!' },
            ]
          },
          programlist: {
            rules: [
              { required: true, message: '请输入方案列表!' },
            ]
          },
          banner: {
            rules: [
              { required: true, message: '请输入banner图!' },
            ]
          },
          analysisdepict: {
            rules: [
              { required: true, message: '请输入分析描述!' },
            ]
          },
          programdepict: {
            rules: [
              { required: true, message: '请输入方案描述!' },
            ]
          },
          analysislist: {
            rules: [
              { required: true, message: '请输入分析列表!' },
            ]
          },
        },
        url: {
          add: '/offweb/mgmSolution/add',
          edit: '/offweb/mgmSolution/edit',
          queryById: '/offweb/mgmSolution/queryById'
        }
      }
    },
    computed: {
      formDisabled() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton() {
        if (this.formBpm === true) {
          if (this.formData.disabled === false) {
            return true
          }
        }
        return false
      }
    },
    created() {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData()
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'createtime', 'updatetime', 'title', 'plan', 'programlist', 'banner', 'analysisdepict', 'programdepict', 'analysislist'))
        })
      },
      //渲染流程表单数据
      showFlowData() {
        if (this.formBpm === true) {
          let params = { id: this.formData.dataId }
          getAction(this.url.queryById, params).then((res) => {
            if (res.success) {
              this.edit(res.result)
            }
          })
        }
      },
      submitForm() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            console.log('表单提交数据', formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
            })
          }

        })
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'createtime', 'updatetime', 'title', 'plan', 'programlist', 'banner', 'analysisdepict', 'programdepict', 'analysislist'))
      },
    }
  }
</script>